package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.UserIntegralEntity;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.StringUtil;

public class IntegralListAdapter extends BaseAdapter {

	public List<UserIntegralEntity> dataList;
	private LayoutInflater inflater;
	private Context context;

	public IntegralListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<UserIntegralEntity>();
		this.inflater = LayoutInflater.from(context);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			IntegralVH integralVH = null;
			if (paramView == null) {
				integralVH = new IntegralVH();
				paramView = this.inflater.inflate(
						R.layout.user_integral_list_item, null);

				integralVH.integralTitleTv = (TextView) paramView
						.findViewById(R.id.tv_integral_item_title);
				integralVH.integralValTv = (TextView) paramView
						.findViewById(R.id.tv_integral_item_val);
				paramView.setTag(integralVH);
			} else {
				integralVH = (IntegralVH) paramView.getTag();
			}

			UserIntegralEntity notice = this.dataList.get(position);
			if (notice != null) {
				//积分标题
				String integerTitle = notice.getIntegralTitle();
				if (!StringUtil.isNullOrEmpty(integerTitle)) {
					integralVH.integralTitleTv.setText(integerTitle);
				} else {
					integralVH.integralTitleTv.setText(context.getResources()
							.getString(R.string.no_text));
				}
				//积分值
				String integerVal = notice.getIntegralVal();
				if (!StringUtil.isNullOrEmpty(integerVal)) {
					integralVH.integralValTv.setText("+"+integerVal+"分");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<UserIntegralEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}

	class IntegralVH {

		TextView integralTitleTv;
		TextView integralValTv;
	}

}
